package com.androidy;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GalleryActivity extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
	JSONArray result = null;
	LinearLayout ablums =null;
	Button back = null;
	View mv = null;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list);
		
		ablums = (LinearLayout)this.findViewById(R.id.cells);	
	    
		TextView header = (TextView)this.findViewById(R.id.header);
		header.setText("My Interest");
		
		back = (Button)this.findViewById(R.id.back);
		back.setOnClickListener(this);
		back.setText("back");
		
		LinearLayout head_layout = (LinearLayout)this.findViewById(R.id.natvigation);
		head_layout.setBackgroundResource(R.drawable.blue);
		
	    new UpdateList().execute();
	    
    }
    
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v == back) {
			GalleryActivity.this.finish();
		} else {
			if (v.getId() != View.NO_ID){
				try {
					String json = result.getJSONObject(v.getId()).getJSONArray("link").getJSONObject(0).getString("href");
					String title = result.getJSONObject(v.getId()).getJSONObject("title").getString("$t");
					Intent i = new Intent(this, image_list.class);
					i.putExtra("json", json);		
					i.putExtra("title", title);		
					mv = v;
				    custAnimationListener cAL = new custAnimationListener("transform", 500, 0, 0, 0, 480, 0);
				    cAL.startAnimation(v, false, i, this);
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public void onResume(){
		super.onResume();
		if (mv != null)
			if (mv.getVisibility()== View.INVISIBLE)
				mv.setVisibility(View.VISIBLE);
	}
	
	public void update_list(){
	    if (result != null) {
		    int JSON_len = result.length();
		    for (int i=0;i<JSON_len; i++)
		    {
		    	LinearLayout child;
				try {
					ImageView thumb = new ImageView(this); 
					child = common.icon_header_cell(this, thumb, result.getJSONObject(i).getJSONObject("title").getString("$t"));
					common.dl_image(thumb, result.getJSONObject(i).getJSONObject("media$group").getJSONArray("media$thumbnail").getJSONObject(0).getString("url"));
					child.setId(i);
			    	child.setOnClickListener(this);
			    	//add cell into layout	
			    	ablums.addView(child);
			    } catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} 
		    }
	    }
	    custAnimationListener cAL = new custAnimationListener("transform", 1000, 0, 0, -800, 0, 0);
	    cAL.startAnimation(ablums, true);
	}
	
	public class UpdateList extends AsyncTask<Void, Void, Void> {
		ProgressDialog progressDialog = null;
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			progressDialog = common.generate_loading_message(GalleryActivity.this);
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			result= dbConnection.getAblumsList("andy0929");
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			if (progressDialog != null)
				progressDialog.dismiss();
			update_list();
		}
	}
	
	//menu
	public boolean onCreateOptionsMenu(Menu menu){
		menu.add(0, 1, 0, "Like Us");   
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item){
	    super.onOptionsItemSelected(item);
	    switch(item.getItemId()){
		    case 1:
		        break;
	    }
        return true;
    }


	

}